'use strict';
const db = uniCloud.database()
const dbCmd = db.command
exports.main = async (event, context) => {
	if (event.leixing == 1) {
		const res = await db.collection('goods_fenlei').get()
		return res
	} else if (event.leixing == 2) {
		const res = await db.collection('goods').doc(event.id).update(event.shuju)
		return res
	} else if (event.leixing == 3) {
		const res = await db.collection('goods').add(event.shuju)
		return res
	} else if (event.leixing == 4) {
		let query = await db.collection('goods').where(event.tiaojian)
		if (event.shaixuan && event.shaixuan.field) {
			query = query.orderBy(event.shaixuan.field, event.shaixuan.order)
		}
		const res = await query.get()
		return res
	} else if (event.leixing == 5) { //有关键词筛选的搜索
		let query = await db.collection('goods').where({
			goods_name: new RegExp(event.key),
			fenlei1_id: event.fenlei1_id
		})
		if (event.shaixuan && event.shaixuan.field) {
			query = query.orderBy(event.shaixuan.field, event.shaixuan.order)
		}
		// 执行查询
		const res = await query.get()
		return res
	}else if (event.leixing == 6) { //有关键词筛选的搜索
		let query = await db.collection('goods').where({
			goods_name: new RegExp(event.key),
			fenlei2_id: event.fenlei2_id
		})
		if (event.shaixuan && event.shaixuan.field) {
			query = query.orderBy(event.shaixuan.field, event.shaixuan.order)
		}
		// 执行查询
		const res = await query.get()
		return res
	}else if (event.leixing == 7) { //有关键词筛选的搜索
		let query = await db.collection('goods').where({
			goods_name: new RegExp(event.key),
			fenlei3_id: event.fenlei3_id
		})
		if (event.shaixuan && event.shaixuan.field) {
			query = query.orderBy(event.shaixuan.field, event.shaixuan.order)
		}
		// 执行查询
		const res = await query.get()
		return res
	}else if (event.leixing == 8) {
		const res = await db.collection('goods_canshu').get()
		return res
	}else if (event.leixing == 9) {
		const res = await db.collection('goods_canshu').where(event.tiaojian).update(event.shuju)
		return res
	}else if (event.leixing == 10) {
		const res = await db.collection('goods_kucun').where(event.tiaojian).get()
		return res
	}else if (event.leixing == 11) {
		const res = await db.collection('goods_kucun').where(event.tiaojian).remove()
		return res
	}else if (event.leixing == 12) {
		const res = await db.collection('goods_kucun').add(event.shuju)
		return res
	}else if (event.leixing == 13) {
		const res = await db.collection('goods').doc(event.id).remove()
		return res
	}else if (event.leixing == 14) {
		const res = await db.collection('gouwuche').add(event.shuju)
		return res
	}else if(event.leixing==15){//获取我的地址
		const res= await db.collection('address').where({id:event.id}).get()
		return {res}
	}else if(event.leixing==16){//保存地址
		let result=await db.collection('address').add({
			id:event.id,
			sheng:event.sheng,
			shi:event.shi,
			qu:event.qu,
			dizhi:event.dizhi,
			lianxiren:event.lianxiren,
			shoujihao:event.shoujihao
		})
		return result
	}else if(event.leixing==17){//删除本条地址记录
		const res= await db.collection('address').where({_id:event._id}).remove()
	}else if(event.leixing==18){//修改地址
		let res=await db.collection('address').where({_id:event._id}).update({
			id:event.id,
			sheng:event.sheng,
			shi:event.shi,
			qu:event.qu,
			dizhi:event.dizhi,
			lianxiren:event.lianxiren,
			shoujihao:event.shoujihao
		})
		return res
	}else if (event.leixing == 19) {
		const res = await db.collection('gouwuche').where(event.tiaojian).remove()
		return res
	}

};